﻿using System.Web.Mvc;
using Newtonsoft.Json;

namespace QunProject.Web.Controllers
{
    /// <summary>
    /// 文章
    /// </summary>
    public class ArticleController : BaseController
    {
        //知识-文章
        public ActionResult Index(int? id)
        {
            ViewBag.ID = id;
            return View();
        }

        //知识-论坛
        public ActionResult Discuss()
        {
            return View();
        }

        //知识-专题
        public ActionResult Special()
        {
            return View();
        }

        //知识-专题-文章列表
        public ActionResult SpecialList(int? id)
        {
            ViewBag.ID = id;
            return View();
        }

        //文章详情
        public ActionResult Info(int id)
        {
            ViewBag.ID = id;
            var article = GetArticleSeoResult(id);
            ViewBag.Article = article;
            ViewBag.Keywords += $",{article.Title}";
            ViewBag.Description = article.Content;
            return View();
        }

        /// <summary>
        /// 获取SEO文章实体
        /// </summary>
        /// <param name="articleId">文章ID</param>
        /// <returns></returns>
        public Model.article GetArticleSeoResult(int articleId)
        {
            var paras = new { articleId };
            var result = HttpPost("GetArticleSeoResult", JsonConvert.SerializeObject(paras));
            var resMo = JsonConvert.DeserializeObject<Common.Result>(result);
            return JsonConvert.DeserializeObject<Model.article>(resMo.data.ToString());
        }

        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="classifyId">知识分类ID</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        public JsonResult GetArticleListResult(int? classifyId, int page = 1, int limit = 10)
        {
            var paras = WeaveParameters<ArticleController>("GetArticleListResult");
            var result = HttpPost("GetArticleListResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }


        /// <summary>
        /// 获取文章实体
        /// </summary>
        /// <param name="articleId">文章ID</param>
        /// <returns></returns>
        public JsonResult GetArticleResult(int articleId, string password = "")
        {
            var paras = WeaveParameters<ArticleController>("GetArticleResult");
            var result = HttpPost("GetArticleResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 获取技术讨论列表
        /// </summary>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        public JsonResult GetDiscussListResult(int page = 1, int limit = 10)
        {
            var paras = WeaveParameters<ArticleController>("GetDiscussListResult");
            var result = HttpPost("GetDiscussListResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 获取专题列表
        /// </summary>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        public JsonResult GetSpecialListResult(int page = 1, int limit = 10)
        {
            var paras = WeaveParameters<ArticleController>("GetSpecialListResult");
            var result = HttpPost("GetSpecialListResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 获取专题文章列表
        /// </summary>
        /// <param name="specialId">专题ID</param>
        /// <returns></returns>
        public JsonResult GetSpecialArticleListResult(int specialId)
        {
            var paras = WeaveParameters<ArticleController>("GetSpecialArticleListResult");
            var result = HttpPost("GetSpecialArticleListResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 获取热门专题列表
        /// </summary>
        /// <returns></returns>
        public JsonResult GetHotSpecialResult()
        {
            var result = HttpPost("GetHotSpecialResult");
            return Json(result);
        }

        /// <summary>
        /// 点赞/踩
        /// </summary>
        /// <param name="type">类型 1|点赞 其他|踩</param>
        /// <param name="articleId">文章ID</param>
        /// <returns></returns>
        public JsonResult EditArticlePraiseCriticismResult(int type, int articleId)
        {
            var paras = WeaveParameters<ArticleController>("EditArticlePraiseCriticismResult");
            var result = HttpPost("EditArticlePraiseCriticismResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 检测文章密码
        /// </summary>
        /// <param name="articleId">文章ID</param>
        /// <returns></returns>
        public JsonResult GetTestArticlePassResult(int articleId)
        {
            var paras = WeaveParameters<ArticleController>("GetTestArticlePassResult");
            var result = HttpPost("GetTestArticlePassResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }
    }
}